package com.qinglei.recoup.system.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTimeUtil {

    private static   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 判断日期是否为1日
     * @param date
     * @return
     */
    public static boolean isFirstDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1);
        if(calendar.get(Calendar.DAY_OF_MONTH) == 2){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 判断当前日期是周几 0 周日
     * @param date
     * @return
     */
    public static int isFirstDayOfWeek(Date date){
        Calendar cal=Calendar.getInstance();
        cal.setTime(new Date());
        int week=cal.get(Calendar.DAY_OF_WEEK)-1;

        return week;
    }

    public static String getPreviousMonday() {
        Calendar cal = Calendar.getInstance();
        // 将每周第一天设为星期一，默认是星期天
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return simpleDateFormat.format(cal.getTime());
    }


    public static String getSunday() {
        Calendar cal = Calendar.getInstance();
        //将每周第一天设为星期一，默认是星期天
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -1*7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return simpleDateFormat.format(cal.getTime());
    }



    public static List<String> getTimeInterval(String oneTime, String twoTime, int minutes) {
        System.out.println("minutes--->>>"+minutes);
        List<String> timeInterval = new ArrayList<>();
        SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");
        Date compareTime = null;
        try {
            compareTime = SDF.parse(twoTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date startTime = null;
        try {
            startTime = SDF.parse(oneTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        timeInterval.add(SDF.format(startTime));

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(startTime);

        if (compareTime.before(startTime)) {
            while (startTime.compareTo(compareTime)>0) {
                if(timeInterval.size()>48){
                    break;
                }
                startTime = dealTime(timeInterval, startTime, rightNow, minutes);
            }
            return timeInterval;
        } else {
            while (!compareTime.before(startTime)) {
                startTime = dealTime(timeInterval, startTime, rightNow, minutes);
            }
            return timeInterval;
        }
    }
    private static Date dealTime(List<String> timeInterval, Date startTime, Calendar rightNow, int minutes) {
        SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");
        rightNow.add(Calendar.MINUTE, minutes);
        startTime = rightNow.getTime();
        timeInterval.add(SDF.format(startTime));
        return startTime;
    }


    /**
     * @description: 两个String类型，按照日期格式对比 eg: dateOne：2015-12-26 dateTwo：2015-12-26
     *               dateFormatType: yyyy-MM-dd 返回类型：-1：dateOne小于dateTwo，
     *               0：dateOne=dateTwo ，1：dateOne大于dateTwo
     * @param dateOne
     * @param dateTwo
     * @param dateFormatType：yyyy-MM-dd
     *            / yyyy-MM-dd HH:mm:ss /等
     * @return -1，0，1，100
     */
    public static int compareTime(String dateOne, String dateTwo, String dateFormatType) {

        DateFormat df = new SimpleDateFormat(dateFormatType);
        Calendar calendarStart = Calendar.getInstance();
        Calendar calendarEnd = Calendar.getInstance();

        try {
            calendarStart.setTime(df.parse(dateOne));
            calendarEnd.setTime(df.parse(dateTwo));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int result = calendarStart.compareTo(calendarEnd);
        if (result > 0) {
            result = 1;
        } else if (result < 0) {
            result = -1;
        } else {
            result = 0;
        }
        return result;
    }


    /**
     * 获取时间区间的所有整点时间
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 整点时间集合
     */
    public static List<String> getHourList(String startTime, String endTime) {


        List<String> hourList = new ArrayList<>();
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat format4 = new SimpleDateFormat("HH:00");
        try {
            Date startTime1 = format2.parse(startTime);
            Date endTime1 = format2.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(startTime1);
            while (endTime1.getTime() >= startTime1.getTime()) {
                hourList.add(format4.format(startTime1));
                //有多种模式可以增加或减去相应的时间
                tempStart.set(Calendar.MINUTE, 0);
                tempStart.add(Calendar.HOUR_OF_DAY, 1);
                startTime1 = tempStart.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hourList;
    }


    public static String addOneDay(String time){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date dd = df.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天
            return df.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 时间转换   格式"['8:00','9:00']"===>list
     * @param time
     * @return
     */
    public static List<String> detalStringListToList(String time){
        List<String> list = new ArrayList<>();
        if(StringUtils.isEmpty(time)){
            return list;
        }
        String replace = time.replace("[", "");
        String replace1 = replace.replace("]", "");
        String replace2 = replace1.replace("'","");
        String[] split = replace2.split(",");
        for (String s : split) {
            list.add(s.trim());
        }
        return list;
    }

    /**
     * 返回5的倍数的时间
     * @param time
     * @return
     */
    public static String detalStringSubTime(String time){
        String lastNum = time.substring(3,5);
        String fristTime = time.substring(0,3);
        String str1 = "";
        if(lastNum.compareTo("15")<0){
            str1 = "00";
        }else if(lastNum.compareTo("15")>=0 &&lastNum.compareTo("30")<0){
            str1 = "15";
        }else if(lastNum.compareTo("30")>=0 &&lastNum.compareTo("45")<0){
            str1 = "30";
        }else if(lastNum.compareTo("45")>=0){
            str1 = "45";
        }else {
            str1 = "59";
        }
        return fristTime + str1;
    }
    /**
     * 返回5的倍数的时间
     * @param time
     * @return
     */
    public static String detalStringFanTime(String time){
        String lastNum = time.substring(3,5);
        String fristTime = time.substring(0,2);
        String str1 = "";
        if(lastNum.compareTo("00")>0 && lastNum.compareTo("15")<=0){
            str1 = "15";
        }else if(lastNum.compareTo("15")>0 &&lastNum.compareTo("30")<=0){
            str1 = "30";
        }else if(lastNum.compareTo("30")>0 &&lastNum.compareTo("45")<=0){
            str1 = "45";
        }else {
            str1 = "00";
            int hour = Integer.valueOf(fristTime) + 1;
            fristTime = String.valueOf(hour);
            if(hour<10){
                fristTime = "0"+hour;
            }
            if("24".equals(fristTime)){
                fristTime = "00";
            }
        }
        return fristTime +":"+ str1;
    }

    public static void main(String[] args) {
        System.out.println(getPreviousMonday());
        System.out.println(getSunday());
    }
}
